package com.lemon.ai.socket.packets.spiritgame.xls2Object.generator;

import java.io.File;
import java.io.FileInputStream;

import com.lemon.ai.socket.packets.spiritgame.model.generator.AbstractGenerator;
import com.lemon.ai.socket.packets.spiritgame.model.utils.FileUtils;
import com.lemon.ai.socket.packets.spiritgame.xls2Object.Constent;

public class ASClassConfigGenerator extends AbstractGenerator {

	private static FileUtils fu = new FileUtils();
	
	public ASClassConfigGenerator() {
		super("asClassConfig.txt");
		
		try {
			this.prop.load(new FileInputStream(new File(System
					.getProperty("user.dir")
					+ "\\templetConfig.properties")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void generate(Constent c) {
		String puts = "";

//			for (int i = 0; i < c.constent.length; i++) {
		for (int i = 0; i < 1; i++) {
			String[] cc = c.constent[i];
			
			if(i == 0) {
				puts += "\t\t\tCLASS_NAME x = new CLASS_NAME();\r\r";
			} else {
				puts += "\t\t\tx = new CLASS_NAME();\r\r";
			}
			
			for (int j = 0; j < c.types.length; j++) {
				String value = cc[j];
				puts += "\t\t\tx." + c.filedNames[j] + " = " + parse(c.types[j], value) +  ";\r" ;
			}
			puts += "\t\t\tdic.[x.KEY_NAME] = x;\r\r";
		}
		
//		String keyType = parseToKeyType(c.types[0]);
		
		String temp = super.temp.
				replaceAll("PACKAGE_NAME", super.prop.getProperty("package")).
				replaceAll("PUTS", puts).
				replaceAll("CLASS_NAME", c.className + "Templet").
//				replaceAll("KEY_TYPE", ASClassGenerator.parse(keyType)).
				replaceAll("KEY_TYPE", ASClassGenerator.parse(c.types[0])).
				replaceAll("KEY_NAME", c.filedNames[0]);
				
		String configName = super.prop.getProperty("dir")
				+ super.prop.getProperty("package").replace(".", "\\")
				 + "\\" + c.className + "TempletConfig"
				+ ".as";
		
		fu.write(configName, temp);
	}

	/**
	 * 转换成包装类
	 * @param string
	 * @return
	 */
	private String parseToKeyType(String type) {
		if(type.equals("int")) {
			return "Integer";
		} else if(type.equals("short")) {
			return "Short";
		} else if(type.equals("float")) {
			return "Float";
		} else if(type.equals("String")) {
			return "String";
		} else if(type.equals("double")) {
			return "Double";
		} else if(type.equals("byte")) {
			return "Byte";
		} else if(type.equals("char")) {
			return "Char";
		} else if(type.equals("boolean")) {
			return "Boolean";
		} else if(type.equals("long")) {
			return "Long";
		} else {
			return "???";
		}
	}

	private String parse(String type, String value) {
		if(type.equals("int")) {
			return value;
		} else if(type.equals("short")) {
//			return "(short)" + value;
			return value;
		} else if(type.equals("float")) {
			return value;
		} else if(type.equals("String")) {
			return "\"" + value + "\"";
		} else if(type.equals("double")) {
			return value;
		} else if(type.equals("byte")) {
//			return "(byte)" + value;
			return value;
		} else if(type.equals("char")) {
//			return "(char)" +value;
			return value;
		} else if(type.equals("boolean")) {
			return value;
		} else if(type.equals("long")) {
			return value;
		} else {
			return "???";
		}
	}

}
